package com.meilingcloud.activiti.controller;

import com.github.pagehelper.Page;
import com.meilingcloud.activiti.domain.dto.ActFormDataSaveDto;
import com.meilingcloud.activiti.domain.dto.ActTaskDto;
import com.meilingcloud.activiti.service.IActTaskService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.PageDomain;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.core.page.TableSupport;
import com.meilingcloud.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {

    private final IActTaskService actTaskService;

    /**
     * 获取我的待办任务
     */
    @PreAuthorize("@ss.hasPermi('activiti:task:list')")
    @GetMapping("/list")
    public TableDataInfo getTasks() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<ActTaskDto> page = actTaskService.selectProcessDefinitionList(pageDomain);
        return getDataTable(page);
    }

    /**
     * 渲染表单
     */
    @PreAuthorize("@ss.hasPermi('activiti:task:query')")
    @GetMapping("/formDataShow/{taskId}")
    public AjaxResult formDataShow(@PathVariable("taskId") String taskId) {
        return AjaxResult.success(actTaskService.formDataShow(taskId));
    }

    @PreAuthorize("@ss.hasPermi('activiti:task:edit')")
    @Log(title = "待办任务 ", businessType = BusinessType.UPDATE)
    @PostMapping("/formDataSave/{taskId}")
    public AjaxResult formDataSave(@PathVariable("taskId") String taskId,
                                   @RequestBody ActFormDataSaveDto params) throws ParseException {
        return toAjax(actTaskService.formDataSave(taskId, params));
    }

    /**
     * 批处理表单数据保存
     *
     * @param taskIds 任务ids
     * @param params  批处理表单数据保存dto
     * @return {@link AjaxResult}
     * @throws ParseException 解析异常
     */
    @PreAuthorize("@ss.hasPermi('activiti:task:edit')")
    @Log(title = "批量审批待办任务 ", businessType = BusinessType.UPDATE)
    @PostMapping("/formDataSave/batch/{taskIds}")
    public AjaxResult batchFormDataSave(@PathVariable("taskIds") List<String> taskIds,
                                        @RequestBody(required = false) ActFormDataSaveDto params) {
        return toAjax(actTaskService.batchFormDataSave(taskIds, params));
    }

}
